package com.mx.zb.mng.ctrl;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mx.zb.mng.bo.Agent;
import com.mx.zb.mng.bo.Operator;
import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.dao.ModuleMapper;
import com.mx.zb.mng.dao.ModuleTypeMapper;
import com.mx.zb.mng.dao.OperatorMapper;
import com.mx.zb.mng.utils.AjaxResult;
import com.mx.zb.mng.utils.Constant;
import com.mx.zb.mng.utils.Crypt;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.ex.PageAnnotation;

/**
 * 
 * @ClassName: IndexCtrl
 * @Description: 登录控制
 * @author 张博
 * @date 2017年9月12日 下午9:39:55 
 *
 */
@Controller
@PageAnnotation
public class IndexCtrl {
	
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	OperatorMapper operatorMapper;
	
	@Autowired
	ModuleTypeMapper moduletypeMapper;
	
	@Autowired
	ModuleMapper moduleMapper;
	
	
	@Autowired
	AgentMapper agentMapper;
	
	@RequestMapping("/")
	public String toLogin() {
		return "login";
	}
	
	@RequestMapping("/login")
	public String login(Operator o, HttpServletRequest request, Model model) {
		String enPass= Crypt.enAes(o.getPassword());
		o.setPassword(enPass);
		o=operatorMapper.validateLogin(o);
		if(o!=null) {
			request.getSession().setAttribute("operatorId", o.getId());
//			request.getSession().setAttribute("tenantCode", o.getTenantCode());
			request.getSession().setAttribute("agentId", o.getAgentId());  
			return "redirect:/p/index";
		}
		
			return "login";
	}
	/**
	 * 登录前异步验证，并友好提示
	 * @author 张衡
	 * @param o
	 * @return
	 */
	@RequestMapping(value= {"/logins"},method= {RequestMethod.POST})
	@ResponseBody
	public AjaxResult loginV(@RequestBody Operator o) {
		AjaxResult result=new AjaxResult();
		result.setStatus("1");
		if (null!=o&&null!=o.getPassword()) {
			String enPass= Crypt.enAes(o.getPassword());
			o.setPassword(enPass);
			Operator os=operatorMapper.validateLogin(o);
			if (null==os) {
				result.setStatus("2");
				result.setMessage("输入的登录信息不正确,请重新登录");
				return result;
			}else {
				String agentId=os.getAgentId();
				Agent agent=agentMapper.selectByPrimaryKey(agentId);
				if(agent!=null) {
					if("0".equals(agent.getIsActive())) {
						result.setStatus("2");
						result.setMessage("账号被禁用,请修改");
					}
				}
			}
		}
		
		return result;
	}
	@RequestMapping("/p/index")
	public String index(Model model) {
		Agent agent=agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());
		 model.addAttribute("agent",JSONObject.toJSON(agent));
		if(Constant.headquarters.equals(agent.getType())) {
			return "dataCalculation";
		}
		return "agentCalculation";
	}
	
}
